package ArraySecond;

public class Array7 {
    /**
     * 给你一幅由 N × N 矩阵表示的图像，其中每个像素的大小为 4 字节。请你设计一种算法，将图像旋转 90 度。
     * <p>
     * 不占用额外内存空间能否做到？
     * <p>
     * <p>
     * 示例 1:
     * <p>
     * 给定 matrix =
     * [
     * [1,2,3],
     * [4,5,6],
     * [7,8,9]
     * ],
     * <p>
     * 原地旋转输入矩阵，使其变为:
     * [
     * [7,4,1],
     * [8,5,2],
     * [9,6,3]
     * ]
     * [1,2,3],
     * [4,5,6],
     * [7,8,9]
     * <p>
     * [1,4,7],
     * [2,5,8],
     * [3,6,9]
     * <p>
     * [7,4,1],
     * [8,5,2],
     * [9,6,3]
     *
     * @param matrix
     */

    public static void rotate(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        //对角线折叠
        for (int i = 0; i < row - 1; i++) {
            for (int j = i + 1; j < col; j++) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = temp;
            }
        }
        //沿着y周对称
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col / 2; j++) {
                int temp = matrix[i][col - 1 - j];
                matrix[i][col - 1 - j] = matrix[i][j];
                matrix[i][j] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        rotate(matrix);
        for (int[] ints : matrix) {
            for (int i = 0; i < ints.length; i++) {
                System.out.print(ints[i]);
            }
            System.out.println();
        }

    }

}
